package com.enjoy.ui;

import org.json.JSONException;
import org.json.JSONObject;

import com.enjoy.R;
import com.enjoy.commons.Commons;
import com.enjoy.utils.GPS;
import com.enjoy.utils.HttpPostHandle;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.ToggleButton;

public class SignUp extends Activity {

	private Button register, cancel;
	private RadioButton male, female;
	private EditText username, password0, password1, mobile;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.signup);
		username = (EditText) findViewById(R.id.username);
		password0 = (EditText) findViewById(R.id.userpassword0);
		password1 = (EditText) findViewById(R.id.userpassword1);
		mobile = (EditText) findViewById(R.id.usermobile);

		male = (RadioButton) findViewById(R.id.male);
		female = (RadioButton) findViewById(R.id.female);
		register = (Button) findViewById(R.id.register);

		register.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				boolean success = true;
				HttpPostHandle handle = new HttpPostHandle(Commons.URL
						+ "serviceName=UserService&operation=insert");
				handle.add("longitude", "123");
				handle.add("latitude", "123");

				if (username.getText().toString().equals("")
						|| (!male.isChecked() && !female.isChecked())
						|| password0.getText().toString().equals("")
						|| password1.getText().toString().equals("")
						|| mobile.getText().toString().equals("")) {
					success = false;
					Toast.makeText(SignUp.this, "请完善信息", Toast.LENGTH_LONG)
							.show();
				} else {
					handle.add("username", username.getText().toString());

					if (male.isChecked()) {
						handle.add("sex", "male");
					} else {
						handle.add("sex", "female");
					}
					
					handle.add("mobile", mobile.getText().toString());
					
					if (password0.getText().toString()
							.equals(password1.getText().toString())) {
						handle.add("password", password0.getText().toString());
					} else {
						success = false;
						Toast.makeText(SignUp.this, "两次密码不一致",
								Toast.LENGTH_LONG).show();

					}
				}

				try {
					JSONObject JSON;
					if (success == true) {
						JSON = handle.getResponse();
						if (JSON.getInt("errorcode") == 1) {
							Toast.makeText(SignUp.this,
									JSON.getString("errormessage"),
									Toast.LENGTH_LONG).show();
						} else {
							Toast.makeText(SignUp.this, "注册成功",
									Toast.LENGTH_LONG).show();
						}
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});

	}
}
